# This Makefile will build the MinGW Win32 application.
TOP_DIR = $(shell pwd)
OBJ_DIR = ${TOP_DIR}/obj

SUB_DIR = core

# Object files to create for the executable
OBJS = obj/AboutDialog.o obj/MainWindow.o obj/Resource.o obj/WinMain.o obj/Star.o obj/bitmap.o obj/cmd_target.o obj/display.o obj/rect.o obj/surface.o obj/theme.o obj/wnd.o obj/word.o obj/api_win.o obj/msg_win.o

export TOP_DIR OBJ_DIR OBJS

# Warnings to be raised by the C compiler
WARNS = -Wall
SYMBOL = -s # Remove file symbol from final binary

# Names of tools to use when building
CC = mingw32-g++.exe
RC = windres
EXE = Win32App.exe

# Compiler flags. Compile ANSI build only if CHARSET=ANSI.
ifeq (${CHARSET}, ANSI)
  CFLAGS = -O2 -std=c++11 -D _WIN32_IE=0x0500 -D WINVER=0x0500 ${WARNS} -Iinclude -ggdb
else
  CFLAGS = -O2 -std=c++11 -D UNICODE -D _UNICODE -D _WIN32_IE=0x0500 -D WINVER=0x0500 ${WARNS} -Iinclude -ggdb
endif

# Linker flags
LDFLAGS = -lgdi32 -luser32 -lkernel32 -lcomctl32 -Wl,--subsystem,windows #${SYMBOL}

.PHONY: all clean

# Build executable by default
all: mkdir ${SUB_DIR} bin/${EXE}

# Run exe
run:
	bin/${EXE}

# Delete all build output
clean:
	rm -rf bin/* obj/*

# Create build output directories if they don't exist
mkdir:
	mkdir -p bin obj

${SUB_DIR}: ECHO
	@make -C $@

ECHO:
	@echo compile ${SUB_DIR}

# Compile object files for executable
obj/%.o: src/%.cpp | obj
	${CC} ${CFLAGS} -c $< -o $@

# Build the resources
obj/Resource.o: res/Resource.rc res/Application.manifest res/Application.ico include/Resource.h | obj
	${RC} -I./include -I./res -i $< -o $@

# Build the exectuable
bin/${EXE}: ${OBJS}
	${CC}  ${OBJS} -o $@ ${LDFLAGS}

# C header dependencies
obj/AboutDialog.o: include/AboutDialog.h include/Globals.h include/Resource.h
obj/MainWindow.o:  include/AboutDialog.h include/Globals.h include/MainWindow.h include/Resource.h
obj/WinMain.o:     include/Globals.h include/MainWindow.h include/Resource.h
